package com.clkj.xiuzheng.dao;

import com.clkj.xiuzheng.pojo.User;
import com.clkj.xiuzheng.pojo.UserImg;
import com.clkj.xiuzheng.pojo.UserSaleAndVisitVo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

/**
 * @author yawang
 * @create 2024/04/12 11:15:56
 **/
@Repository
public interface UserMapper extends Mapper<User> {
    @Insert("insert into t_cl_user values (#{userName},#{openId},#{userTel},#{userTag},#{userStatus},#{userPassword},#{companyId},#{userBalance})")
    @Options(useGeneratedKeys = true, keyProperty = "userId", keyColumn = "user_id")
    void addUser(User user);
    @Select("select * from t_cl_user ")
    List<User> getAllUser();
    @Select("SELECT TOP 3 o.user_id AS user_id,SUM(o.total_money) AS total_money,u.user_name AS user_name FROM  t_cl_outStock o LEFT JOIN t_cl_user u ON o.user_id = u.user_id where o.company_id=#{companyId} and dateDiff(day,o.out_date,getDate()) <=1 GROUP BY o.user_id, u.user_name  ORDER BY total_money DESC ")
    List<UserSaleAndVisitVo> getUserSaleRanking(@Param("companyId") Integer companyId);
    @Select("SELECT TOP 3 c.user_id AS user_id,COUNT(c.visit_id) AS total_visit,u.user_name AS user_name FROM t_cl_customerVisit c LEFT JOIN t_cl_user u ON c.user_id = u.user_id where c.company_id=#{companyId} and dateDiff(day,c.visit_date,getDate()) <=1 GROUP BY c.user_id,u.user_name ORDER BY total_visit desc ")
    List<UserSaleAndVisitVo> getUserVisitRanking(@Param("companyId") Integer companyId);
//    @Select("select user_id,user_name,user_status from t_cl_user where user_tag = -1 ")
//    List<User> getSaleUser();
//    @Insert("insert into t_cl_user (user_name,user_tel,user_tag,user_status) values (#{userName},#{userTel},-1,0)")
//    void addSaleUser(User user);
//    @Update("update t_cl_user set user_status = 1 where user_id = #{userId}")
//    void updateUserUp(Integer userId);
//    @Update("update t_cl_user set user_status = 0 where user_id = #{userId}")
//    void updateUserDown(Integer userId);


    @Insert("insert into t_cl_userImg values(#{userId},#{userImg})")
    void addImg(UserImg userImg);

    @Select("select * from t_cl_userImg where user_id = #{userId}")
    UserImg selectUserImg(@Param("userId") Integer userId);


    @Select("<script>" +
            "select b.user_id,a.user_name,a.user_balance,count(*) as openId from " +
            "t_cl_user a left join t_cl_shopVisit b on a.user_id = b.user_id " +
            "where DATEDIFF(MONTH,b.visit_date,#{time})= 0  " +
            "<if test='userId!=0'> and a.user_id = #{userId}</if>" +
            " group by b.user_id,a.user_name,a.user_balance" +
            "</script>")
    List<User> listGMoney(String time, Integer userId);

}
